var ClubSelectWanfaUnit = Unit.extend({
    _ccsFileName: res.ClubSelectWanfaUnit_json,
    _clubId: null,
    _wanfaFloorNum: 4, //玩法楼层总数
    _selectFloor: 0,  //选中的楼层  0为默认选中混合玩法
    initUnit: function () {
        this._nodeList['wanfaDetailsPanel'].setVisible(false);
        // this._nodeList['detailsLabel'].setVisible(false);
        this._nodeList['topBeiZhuLabel'].setString('');
        this._nodeList['selectWanfaName'].setString('');
        this._nodeList['selectWanfaDesp'].setString('');
        this._normalBlendWanfaView();
    },
    /**
     * View基类初始化
     */
    initView: function () {
        this._actionType = ViewActionType.None;
        if (this._actionType === ViewActionType.None) {
            this._rootNode.show();
        }
        this._loadCCSFinished = true;
        this.setData.apply(this, arguments);
    },
    onEnter: function () {
        Unit.prototype.onEnter.call(this);
        this._initAllListeners();
    },
    _initClickListeners: function () {
        this._clickListeners = {
            'btn_changeWanfa': '_clickChangeWanfa',
            'detailsLabel': '_clickDetailsLabel',
            'wanfaDetailsPanel.touchCloseLayer': '_clickTouchCloseLayer',
            'bg_wafaBack': '_clickChangeRemarksTop',
        };
        this._nodeList['wanfaDetailsPanel.wanfa0'].floor = 0;
        this._clickListeners['wanfaDetailsPanel.wanfa0'] = '_clickSelctFloor';
        for (var i = 1; i <= this._wanfaFloorNum; i++) {
            var str = 'wanfaDetailsPanel.wanfa' + i;
            this._nodeList[str + '.btn_addWanfa'].floor = i;
            this._nodeList[str + '.btn_remove'].floor = i;
            this._nodeList[str + '.btn_xiugai'].floor = i;
            this._nodeList[str + '.touchChangeName'].floor = i;
            this._nodeList[str + '.beiZhuLabel'].floor = i;
            this._nodeList[str].floor = i;
            this._clickListeners[str] = '_clickSelctFloor';
            this._clickListeners[str + '.btn_addWanfa'] = '_clickAddFloor';
            this._clickListeners[str + '.btn_remove'] = '_clickRemoveFloor';
            this._clickListeners[str + '.btn_xiugai'] = '_clickChangeFloorWanfa';
            this._clickListeners[str + '.touchChangeName'] = '_clickChangeRemarks';
            this._clickListeners[str + '.beiZhuLabel'] = '_clickChangeRemarks';
        }
    },

    /**
     * 监听亲友圈消息
     * @private
     */
    _initEventListeners: function () {
        this._eventListeners = {};
        //监听 界面 事件
        this._eventListeners[ClubEvent.ClubBaseInfo] = '_clubBaseInfoHandler';
        this._eventListeners[ClubEvent.QueryWanfas] = '_queryWanfasHandler';

        this._eventListeners[ClubMsgCode.QueryWanfas] = '_refreshView';
    },

    _clubBaseInfoHandler: function (clubId) {
        this._clubId = clubId;
        this._refreshSelectFloorView();
    },


    /**
     * 刷新玩法显示
     * @private
     */
    _queryWanfasHandler: function () {
        if (this._nodeList['wanfaDetailsPanel'].isVisible()) {
            this._refreshView();
        }
        this._refreshSelectFloorView();
    },

    /**
     * 点击显隐玩法
     * @private
     */
    _clickChangeWanfa: function () {
        this._selectFloor = 0;
        var isView = !this._nodeList['wanfaDetailsPanel'].isVisible();
        this._refreshSelectFloorView();
        if (this._clubId == null || !isView) {
            this._nodeList['wanfaDetailsPanel'].setVisible(isView);
            return;
        }

        clubMsgManager.requestClubWanFa(this._clubId, true);
        // this._refreshView();
        //发送关闭左侧 成员 牌桌 快捷面板
        eventManager.dispatchEvent(ClubEvent.ClubCloseTableMemeberUnit, this._selectFloor);
    },

    _clickDetailsLabel: function () {
        showWindow('ClubWanFaDetailsWindow', this._clubId, this._selectFloor);
    },

    /**
     * 点击透明区域
     * @private
     */
    _clickTouchCloseLayer: function () {
        this._nodeList['wanfaDetailsPanel'].setVisible(false);
    },

    /**
     * 点击选择楼层
     * @private
     */
    _clickSelctFloor: function (target) {
        this._selectFloor = target.floor;
        //永远设置成默认0，混合玩法
        this._selectFloor = 0;
        this._nodeList['wanfaDetailsPanel'].setVisible(false);
        this._refreshSelectFloorView();
        eventManager.dispatchEvent(ClubEvent.SelectFloor, this._selectFloor);
    },

    /**
     * 添加楼层玩法
     * @private
     */
    _clickAddFloor: function (target) {
        this._selectFloor = target.floor;
        //永远设置成默认0，混合玩法
        this._selectFloor = 0;
        var floor = target.floor;
        var isSetWanfa = (floor != 5);
        var isDaiKai = (floor == 5);
        club.openCreateRoomWindow(this._clubId, floor, isDaiKai, isSetWanfa);
        // showWindow('CreateRoomWindow', 'hall', isDaiKai, this._clubId, isSetWanfa, floor);
        eventManager.dispatchEvent(ClubEvent.SelectFloor, this._selectFloor);


    },

    /**
     * 删除楼层玩法
     * @private
     */
    _clickRemoveFloor: function (target) {
        var floor = target.floor;
        showMessageBox('是否确定删除玩法设置?', function () {
            network.send(2103, {cmd: ClubMsgCode.DelWanfa, club_id: this._clubId, pos: floor});
        }.bind(this));
    },

    /**
     * 修改楼层玩法
     * @private
     */
    _clickChangeFloorWanfa: function (target) {
        this._selectFloor = target.floor;
        var floor = target.floor;
        //永远设置成默认0，混合玩法
        this._selectFloor = 0;
        showWindow('ClubWanFaDetailsWindow', this._clubId, floor);
    },


    _clickChangeRemarksTop: function () {
        if (this._selectFloor == 0) {
            return;
        }
        this._clickChangeRemarks({floor: this._selectFloor});
    },

    /**
     * 修改楼层备注
     * @private
     */
    _clickChangeRemarks: function (target) {
        var floor = target.floor;
        if (!ClubDataManager.isSelfAdministration(this._clubId, gameData.uid)) {
            return;
        }
        showWindow('ClubInputWindow', '最多输入8个字', ClubInputWindowType.ChangeRemarks, function (inputStr) {
            if (inputStr.length > 8) {
                showAlert('备注名称最长8个字');
                return;
            }
            network.send(2103, {cmd: ClubMsgCode.ModWanfa, club_id: this._clubId, pos: floor, remarks: inputStr});
        }.bind(this), 8);
    },


    /**
     * 刷新选中了楼层显示
     * @private
     */
    _refreshSelectFloorView: function () {
        var tips = '点击修改备注';
        if (!ClubDataManager.isSelfAdministration(this._clubId, gameData.uid)) {
            tips = '暂无备注';
        }
        var wanfaInfo = this._getPosWanfaInfo(this._selectFloor);
        if (wanfaInfo) {
            tips = wanfaInfo.remarks || tips;
            var str = wanfaInfo.renShu + '人,';
            if (wanfaInfo.hunzhuo) {
                str = '混桌 ';
            }
            str += wanfaInfo.jushu + '局';
            this._nodeList['topBeiZhuLabel'].setString(tips);
            //this._nodeList['selectWanfaName'].setString(wanfaInfo.name);
            //this._nodeList['selectWanfaDesp'].s)etString(str);
            //暂时屏蔽bg_wafaback topBeiZhuLabel
            //this._nodeList['bg_wafaBack'].setVisible(true);
            //this._nodeList['topBeiZhuLabel'].setVisible(true);
        } else {
            this._normalBlendWanfaView();
        }
    },

    /**
     * 混合玩法默认显示信息
     * @private
     */
    _normalBlendWanfaView: function () {
        //this._nodeList['selectWanfaName'].setString('混合玩法');
        this._nodeList['selectWanfaDesp'].setString('');
        this._nodeList['bg_wafaBack'].setVisible(false);
        this._nodeList['topBeiZhuLabel'].setVisible('');
    },

    _checkFloor: function (pos) {
        if (pos < ClubDataManager.FloorMin || pos > ClubDataManager.FloorMax) {
            return false;
        }
        return true;
    },

    _refreshView: function () {
        this._nodeList['wanfaDetailsPanel'].setVisible(true);
        var wanfas = ClubDataManager.getClubWanfas(this._clubId);
        var tips = '点击修改备注';
        if (!ClubDataManager.isSelfAdministration(this._clubId, gameData.uid)) {
            tips = '暂无备注';
        }

        var i = 0;
        for (i = 0; i <= this._wanfaFloorNum; i++) {
            if (i != 0)
                this._wanFaNodeChildeViewHide('wanfaDetailsPanel.wanfa' + i, false);
            this._nodeList['wanfaDetailsPanel.wanfa' + i + '.selectsStyle'].setVisible(false);
        }
        for (i = 0; i < wanfas.length; i++) {
            var wanfa = wanfas[i];
            var wanfaInfo = this._getPosWanfaInfo(wanfa.pos);
            if (!this._checkFloor(wanfa.pos) || !wanfaInfo) {
                continue;
            }
            var wanfaNodeStr = 'wanfaDetailsPanel.wanfa' + wanfa.pos;
            this._wanFaNodeChildeViewHide(wanfaNodeStr, true);


            var str = wanfaInfo.renShu + '人,';
            if (wanfaInfo.hunzhuo) {
                str = '混桌 ';
            }
            str += wanfaInfo.jushu + '局';
            this._nodeList[wanfaNodeStr + '.onLineLabel'].setString(wanfaInfo.roomMemberNum + '人在桌');
            this._nodeList[wanfaNodeStr + '.wanfaDesp'].setString(str);
            this._nodeList[wanfaNodeStr + '.wanfaName'].setString(wanfaInfo.name);
            this._nodeList[wanfaNodeStr + '.beiZhuLabel'].setString(wanfaInfo.remarks || wanfaInfo.name || tips);
            this._nodeList[wanfaNodeStr + '.btn_changeName'].setVisible(ClubDataManager.isSelfAdministration(this._clubId, gameData.uid));
            this._nodeList[wanfaNodeStr + '.touchChangeName'].setVisible(ClubDataManager.isSelfAdministration(this._clubId, gameData.uid));
        }
        this._nodeList['wanfaDetailsPanel.wanfa' + this._selectFloor + '.selectsStyle'].setVisible(true);
    },

    /**
     * 获取指定楼层显示用的玩法信息
     * @param pos
     */
    _getPosWanfaInfo: function (pos) {
        var info = null;
        var wanfas = ClubDataManager.getClubWanfas(this._clubId);
        for (var i = 0; i < wanfas.length; i++) {
            var room = wanfas[i];
            if (room.pos == pos) {
                info = room;
                break;
            }
        }
        if (info == null) {
            // showAlert('亲友圈:' + this._clubId + " 楼层:" + pos + " 没有找到玩法信息");
            return null;
        }

        var roomList = ClubDataManager.getClubFloorRomList(this._clubId, pos);
        var roomMemberNum = 0;
        //获取当前楼层 在桌内的玩家数
        for (var i = 0; i < roomList.length; i++) {
            roomMemberNum += roomList[i]['cur_player_cnt'] || roomList[i]['heads'].length;
        }

        var option = JSON.parse(info.options);
        var playerMap = ClubDataManager.analysisWanFaInfo(option)['playerMap'];
        info.desc = decodeURIComponent(info.desc);
        var returnObj = {};
        returnObj.name = ClubDataManager.getDesc2WanfaName(info);
        returnObj.desc = decodeURIComponent(info.desc);
        returnObj.jushu = option.jushu;
        returnObj.roomMemberNum = roomMemberNum;
        var keys = _.keys(playerMap);
        if (keys.length > 1) {
            returnObj.hunzhuo = true;
        } else if (keys.length == 1) {
            returnObj.renShu = keys[0];
        } else {
            returnObj.renShu = 0;
        }
        returnObj.remarks = info.remarks;
        return returnObj;
    },

    //左侧玩法子节点显隐操作
    _wanFaNodeChildeViewHide: function (wanfaNodeStr, hide) {
        this._nodeList[wanfaNodeStr + '.wanfaName'].setVisible(hide);
        this._nodeList[wanfaNodeStr + '.onLineLabel'].setVisible(false);
        this._nodeList[wanfaNodeStr + '.wanfaDesp'].setVisible(false);
        this._nodeList[wanfaNodeStr + '.beiZhuLabel'].setVisible(hide);
        this._nodeList[wanfaNodeStr + '.btn_changeName'].setVisible(hide);
        this._nodeList[wanfaNodeStr + '.touchChangeName'].setVisible(hide);
        this._nodeList[wanfaNodeStr + '.btn_xiugai'].setVisible(hide);
        this._nodeList[wanfaNodeStr + '.btn_remove'].setVisible(hide);
        this._nodeList[wanfaNodeStr + '.floor'].setVisible(hide);
        this._nodeList[wanfaNodeStr + '.btn_addWanfa'].setVisible(!hide);
        if (!ClubDataManager.isSelfAdministration(this._clubId, gameData.uid)) {
            this._nodeList[wanfaNodeStr + '.btn_xiugai'].setVisible(false);
            this._nodeList[wanfaNodeStr + '.btn_remove'].setVisible(false);
            this._nodeList[wanfaNodeStr + '.btn_addWanfa'].setVisible(false);
        }
    },


});